<%= form_for @reply, html: { multipart: true, class: 'new_reply' } do |r| %>
  <%= r.hidden_field :content_type, value: current_user.prefer_plain_text? ? 'text' : 'html' %>

  <%= r.hidden_field :ticket_id %>

  <div class="row collapse bb">
    <div class="medium-1 column ptl pll">
      <%= user_avatar current_user, class: 'avatar', size: 35 %>
    </div>
    <div class="medium-11 columns ptm pbm pll prl">
      <h3 class="text-default"><%= t(:reply) %></h3>
      <% if current_user.agent? %>
        <%= r.radio_button :internal, false, label: t(:reply), data: { remote: true, url: new_reply_path, params: "reply[ticket_id]=#{@reply.ticket.id}" } %>
        <%= r.radio_button :internal, true, label: t(:internal_note), data: { remote: true, url: new_reply_path, params: "reply[ticket_id]=#{@reply.ticket.id}" } %>
      <% end %>
      <% if can? :update, @reply.ticket %>
        <div class="row collapse">
          <%= r.fields_for :ticket do |f| %>
            <div class="medium-4 columns">
              <%= f.label :to_email_address_id, t('activerecord.attributes.reply.from'), class: 'ptm' %>
            </div>
            <div class="medium-8 columns">
              <% blank = @outgoing_addresses.count == 0 ? EmailAddress.default_email : '' %>
              <%= f.collection_select :to_email_address_id, @outgoing_addresses, :id, :formatted,
                  include_blank: blank, label: false %>
            </div>
          <% end %>
        </div>
      <% end %>

      <div class="row collapse mbl">
        <div class="medium-4 columns">
          <label><%= t(:notification_will_be_sent_to) %></label>
        </div>
        <div class="medium-8 columns">
          <%= render 'replies/notified_users', r: r %>
        </div>
      </div>

      <% if (@canned_replies = EmailTemplate.canned_replies.order(:name)).any? %>
        <div class="row collapse mbl">
          <div class="medium-4 columns">
            <label><%= t(:canned_reply) %></label>
          </div>
          <div class="medium-8 columns">
            <%= render 'replies/canned_replies' %>
          </div>
        </div>
      <% end %>

      <%
        if @reply.content.blank? && @reply.errors.count == 0 # not on reply error view

          if current_user.prefer_plain_text?
            @reply.content = "\n#{html_to_text current_user.signature}\n"
          else
            @reply.content = "<p></p>#{current_user.signature}<br />"
          end

          if current_user.include_quote_in_reply?
            @reply.content += t(:on_date_author_wrote, author: @reply.reply_to.user.email,
                date: l(@reply.reply_to.created_at.in_time_zone(current_user.time_zone), format: :long))

            if current_user.prefer_plain_text?
              @reply.content += "\n#{wrap_and_quote(@reply.reply_to.content)}"
            else
              @reply.content += "<br />#{text_to_html(wrap_and_quote(@reply.reply_to.content))}"
            end
          end
        end
      %>

      <p>
      <% if current_user.prefer_plain_text? %>
        <%= r.text_area :content, label: false %>
      <% else %>
        <%= r.trix_editor :content, label: false %>
      <% end %>
      </p>

      <%= render 'attachments/form', f: r %>

      <% if can? :update, @reply.ticket %>
        <div class="row collapse">
          <%= r.fields_for :ticket do |f| %>
            <div class="medium-4 columns">
              <%= f.label :status, t(:change_status), class: 'ptm' %>
            </div>
            <div class="medium-8 columns">
              <%= f.collection_select(:status,
                  t('activerecord.attributes.ticket.statuses'), :first, :last,
                  label: false) %>
            </div>
          <% end %>
        </div>
      <% end %>

      <p>
        <%= r.button t(:save_as_draft), name: 'reply[draft]', value: true, class: 'button regular radius primary' %>

        <%= r.button @reply.internal? ? t(:attach_note) : t(:send_reply), name: 'reply[draft]', value: false, type: :submit, class: 'reply-submit button regular radius secondary' %>
      </p>

    </div>
  </div>
<% end %>
